﻿/**
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
namespace IPresenter
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
   
    /// <summary>
    /// Import API functions from library user32.dll
    /// </summary>
    public static class ApiFunction
    {
        /// <summary>
        /// code of event keydown
        /// </summary>
        public const int KEY_DOWN = 0x0100;

        /// <summary>
        /// code of event keyup
        /// </summary>
        public const int KEY_UP = 0x0101;

        /// <summary>
        /// Get handle window which is being activated
        /// </summary>
        /// <returns>Handle of window</returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int GetForegroundWindow();

        /// <summary>
        /// Get handle window which is being activated in this thread
        /// </summary>
        /// <returns>Handle of window</returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int GetActiveWindow();

        /// <summary>
        /// Find window follow class name
        /// </summary>
        /// <param name="className">class name</param>
        /// <param name="captionWindow">window caption</param>
        /// <returns>Handle of window</returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int FindWindow(string className, IntPtr captionWindow);

        /// <summary>
        /// Find window( extend)
        /// </summary>
        /// <param name="handleParent">Handle of parent window</param>
        /// <param name="handleChild">Handle of child window</param>
        /// <param name="className">Class name</param>
        /// <param name="captionWindow">Caption window</param>
        /// <returns>Handle of window</returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int FindWindowEx(int handleParent, IntPtr handleChild, string className, IntPtr captionWindow);

        /// <summary>
        /// Find window( extend)
        /// </summary>
        /// <param name="handleParent">Handle of parent window</param>
        /// <param name="handleChild">Handle of child window</param>
        /// <param name="className">Class name</param>
        /// <param name="captionWindow">Caption window</param>
        /// <returns>Handle of window</returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int FindWindowEx(int handleParent, int handleChild, string className, IntPtr captionWindow);

        /// <summary>
        /// Send message to another window
        /// </summary>
        /// <param name="handle"> Handle window which you want to send </param>
        /// <param name="message"> Kind of event </param>
        /// <param name="key"> Key which you would like to send </param>
        /// <param name="scanCode"> ScanCode of key </param>
        [DllImport("user32.dll")]
        public static extern void PostMessage(int handle, int message, Keys key, long scanCode);

        /// <summary>
        /// Get caption of window
        /// </summary>
        /// <param name="handle">Handle window</param>
        /// <param name="caption">String receive result</param>
        /// <param name="maxLength">Max length of result</param>
        /// <returns>Size of caption</returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int GetWindowText(int handle, StringBuilder caption, int maxLength);

        /// <summary>
        ///  Get class name of window
        /// </summary>
        /// <param name="handle">Handle window</param>
        /// <param name="className">String receive result</param>
        /// <param name="maxLength">Max length of result</param>
        /// <returns>Size of class name</returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int GetClassName(int handle, StringBuilder className, int maxLength);

        /// <summary>
        /// Set active window
        /// </summary>
        /// <param name="handleWindow">Handle window</param>
        /// <returns>
        /// true: success
        /// false: fail
        /// </returns>
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool SetForegroundWindow(int handleWindow);
    }
}